/**
 * 
 */
package co.fitstart.mobile.entity.trading;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;

import com.demo2do.core.utils.DateFormatUtils;

import co.fitstart.entity.coupon.Coupon;
import co.fitstart.entity.order.Order;
import co.fitstart.entity.product.ProductType;
import co.fitstart.entity.reference.Contact;
import co.fitstart.entity.user.User;

/**
 * @author downpour
 *
 */
public class Shopping {
    
    /** 下单用户 */
    private Long userId;
    
    private Contact contact;
    
    /** 二维码图片地址 */
    private String qrcode;
    
    /** 下单产品 */
    private Long productId;
    
    private Long siteId;
    
    /** 购买数量 */
    private int amount;
    
    /** 优惠券 */
    private Long couponId;
    
    /** 折扣码 */
    private String discountCode;
    
    /** 产品类型 */
    private String type;
    
    /** 用户留言 */
    private String comment;
    
    /** 身份证号 */
    private String identificationCard;
    
    /** 地址 */
    private String address;
    
    /** 联系人微信号  */
    private String wechatNumber;
    
    /** 身高 */
    private Integer height;
    
    /** 体重 */
    private Float weight;
    
    /** 时差 */
    private Integer hourDifference;
    
    /**
     * 
     */
    public Shopping() {
        
    }
    
    /**
     * 
     * @param user
     * @param coupon
     * @param siteId
     */
    public Shopping(User user, Coupon coupon, Long siteId) {
        this.userId = user.getId();
        this.contact = new Contact(coupon.getContactPerson(), coupon.getIdentification());
        this.wechatNumber = coupon.getWechatNumber();
        this.productId = coupon.getTemplate().getProduct().getId();
        this.siteId = siteId;
        this.amount = 1;
        this.couponId = coupon.getId();
        this.type = coupon.getTemplate().getProduct().getType().getAlias();
        
    }
    
    /**
     * 
     * @param userId
     * @return
     */
    public Shopping user(Long userId) {
        this.userId = userId;
        return this;
    }

    /**
     * 
     * @return
     */
    public Order createOrder() {
        return new Order(amount);
    }
    
    /**
     * 
     * @return
     */
    public String createOrderNo() {
        
        // TODO add header sign
        
        // 2 digits random number between 0 (inclusive) ~ 100 (exclusive)
        String randomNumber = String.format("%02d", Double.valueOf(Math.floor(Math.random() * 100)).intValue());
        
        // Generate code with 20 digits total
        return new StringBuilder().append("W")
                                  .append(randomNumber)
                                  .append(DateFormatUtils.format(new Date(), "yyMMddHHmmssSSS"))
                                  .toString();
    }
    
    /**
     * 
     * @return
     */
    public boolean withCoupon() {
        return this.couponId != null;
    }
    
    /**
     * 
     * @return
     */
    public boolean withDiscountCode() {
    	return StringUtils.isNoneBlank(this.discountCode);
    }
    
    /**
     * 
     * @return
     */
    public boolean withSite() {
        return this.siteId != null;
    }
    
    /**
     * 
     * @return
     */
    public boolean withType() {
        ProductType productType = ProductType.aliasOf(this.type);
        return productType == null ? false : true;
    }
    
    /**
     * 
     * @return
     */
    public boolean withComment() {
    	return StringUtils.isNotBlank(comment) && StringUtils.isNotEmpty(comment);
    }
    
    /**
     * 
     * @return
     */
    public boolean withOthers() {
        return (StringUtils.isNotBlank(identificationCard) && StringUtils.isNotEmpty(identificationCard)) || (StringUtils.isNotBlank(address) && StringUtils.isNotEmpty(address));
    }

    /**
     * @return the userId
     */
    public Long getUserId() {
        return userId;
    }

    /**
     * @return the contact
     */
    public Contact getContact() {
        return contact;
    }

    /**
     * @return the qrcode
     */
    public String getQrcode() {
        return qrcode;
    }

    /**
     * @return the productId
     */
    public Long getProductId() {
        return productId;
    }

    /**
     * @return the siteId
     */
    public Long getSiteId() {
        return siteId;
    }

    /**
     * @return the amount
     */
    public int getAmount() {
        return amount;
    }

    /**
     * @return the couponId
     */
    public Long getCouponId() {
        return couponId;
    }

    /**
     * @return the discountCode
     */
    public String getDiscountCode() {
        return discountCode;
    }

    /**
     * @return the type
     */
    public String getType() {
        return type;
    }

    /**
     * @return the comment
     */
    public String getComment() {
        return comment;
    }

    /**
     * @return the identificationCard
     */
    public String getIdentificationCard() {
        return identificationCard;
    }

    /**
     * @return the address
     */
    public String getAddress() {
        return address;
    }

    /**
     * @return the wechatNumber
     */
    public String getWechatNumber() {
        return wechatNumber;
    }

    /**
     * @return the height
     */
    public Integer getHeight() {
        return height;
    }

    /**
     * @return the weight
     */
    public Float getWeight() {
        return weight;
    }

    /**
     * @return the hourDifference
     */
    public Integer getHourDifference() {
        return hourDifference;
    }

    /**
     * @param userId the userId to set
     */
    public void setUserId(Long userId) {
        this.userId = userId;
    }

    /**
     * @param contact the contact to set
     */
    public void setContact(Contact contact) {
        this.contact = contact;
    }

    /**
     * @param qrcode the qrcode to set
     */
    public void setQrcode(String qrcode) {
        this.qrcode = qrcode;
    }

    /**
     * @param productId the productId to set
     */
    public void setProductId(Long productId) {
        this.productId = productId;
    }

    /**
     * @param siteId the siteId to set
     */
    public void setSiteId(Long siteId) {
        this.siteId = siteId;
    }

    /**
     * @param amount the amount to set
     */
    public void setAmount(int amount) {
        this.amount = amount;
    }

    /**
     * @param couponId the couponId to set
     */
    public void setCouponId(Long couponId) {
        this.couponId = couponId;
    }

    /**
     * @param discountCode the discountCode to set
     */
    public void setDiscountCode(String discountCode) {
        this.discountCode = discountCode;
    }

    /**
     * @param type the type to set
     */
    public void setType(String type) {
        this.type = type;
    }

    /**
     * @param comment the comment to set
     */
    public void setComment(String comment) {
        this.comment = comment;
    }

    /**
     * @param identificationCard the identificationCard to set
     */
    public void setIdentificationCard(String identificationCard) {
        this.identificationCard = identificationCard;
    }

    /**
     * @param address the address to set
     */
    public void setAddress(String address) {
        this.address = address;
    }

    /**
     * @param wechatNumber the wechatNumber to set
     */
    public void setWechatNumber(String wechatNumber) {
        this.wechatNumber = wechatNumber;
    }

    /**
     * @param height the height to set
     */
    public void setHeight(Integer height) {
        this.height = height;
    }

    /**
     * @param weight the weight to set
     */
    public void setWeight(Float weight) {
        this.weight = weight;
    }

    /**
     * @param hourDifference the hourDifference to set
     */
    public void setHourDifference(Integer hourDifference) {
        this.hourDifference = hourDifference;
    }
}
